package com.zc.emos.wx.config.shiro;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author 曾川
 * @version 1.0.0
 * @ClassName ShiroConfig.java
 * @Description
 * 配置类，类似于xml的，添加上注解
 * 将realm和filter添加到shiro框架中，
 *
 * Spring的@Bean注解用于告诉方法，产生一个Bean对象，
 * 然后这个Bean对象交给Spring管理。产生这个Bean对象的方法Spring只会调用一次，
 * 随后这个Spring将会将这个Bean对象放在自己的IOC容器中。
 * @createTime 2021年11月30日 15:24:00
 */
@Configuration
public class ShiroConfig {

    /*封装realm对象
    realm，用来授权、认证的类
    securityManager封装realm成为对象
    realm用于验证授权
    注册给spring框架*/
    @Bean("securityManager")
    public SecurityManager securityManager(OAuth2Realm realm) {
        /*SecurityManager的子类DefaultWebSecurityManager*/
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        /*封装封装realm对象*/
        securityManager.setRealm(realm);
        securityManager.setRememberMeManager(null);
        /*返回securityManager*/
        return securityManager;
    }

    /*用于封装filter对象、设置filter拦截路径*/
    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager, OAuth2Filter filter) {
        /**shiroFilterFactoryBean类中的shiroFilter对象可以执行setSecurityManager方法，
         * 来接收上面写过的securityManager方法*/
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);

        /**接下来，要在ShiroFilterFactoryBean 中封装Filter对象，但是Filter类还不能直接封装进去。
         需要引入map对象才行。
         下面是创建map对象，并把Filter类创建出来的oAuth2Filter对象放入map里，起名为oauth2。*/
        Map<String, Filter> map = new HashMap<>();
        map.put("oauth2", filter);
        shiroFilter.setFilters(map);

        /*定义是否拦截，用hashmap中的子类LinkedHashMap，顺序记录拦截规则
        为保证数据的插入顺序和保存的书序一致，选用LinkedHashMap类。
         map函数通过<key,vaule>的格式保存数据，anon声明数据要不要被拦截。
         filterMap.put("/**", "oauth2");表示除了上述的路径，其他的都要拦截。*/
        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/webjars/**", "anon");
        filterMap.put("/druid/**", "anon");
        filterMap.put("/app/**", "anon");
        filterMap.put("/sys/login", "anon");
        filterMap.put("/swagger/**", "anon");
        filterMap.put("/v2/api-docs", "anon");
        filterMap.put("/swagger-ui.html", "anon");
        filterMap.put("/swagger-resources/**", "anon");
        filterMap.put("/captcha.jpg", "anon");
        filterMap.put("/user/register", "anon");
        filterMap.put("/user/login", "anon");
        filterMap.put("/test/**", "anon");
        filterMap.put("/meeting/recieveNotify", "anon");
        filterMap.put("/**", "oauth2");

        /*将filterMap*/
        shiroFilter.setFilterChainDefinitionMap(filterMap);

        return shiroFilter;

    }

    /*管理shiro对象生命周期*/
    @Bean("lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }


    /*AOP切面类、web方法执行前、验证权限
    即@bean，添加的bean的id为方法名
    @bean里面写不写都行，默认会按照返回值的首字母小写然后注册，即默认就是@Bean("authorizationAttributeSourceAdvisor")*/
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }
}
